# =============================================================================
# run_all.R
# Purpose   : Master script to reproduce all analyses (replication version)
# Author    : Malo JAN
# -----------------------------------------------------------------------------
# Description:
# This script executes all data cleaning and analysis scripts in order to
# reproduce the results of the project. It automatically:
#   1. Runs each R script under code/, in sequence.
#   2. Logs any errors to outputs/logs/run_all_errors_<timestamp>.log.
#   3. Stops if required data are missing.
# =============================================================================

# --- 2. Load base helper packages --------------------------------------------
library(purrr)
library(glue)
library(here)

message("\n===== Starting replication pipeline =====")

# --- 3. Prepare error log file -----------------------------------------------
timestamp <- format(Sys.time(), "%Y-%m-%d_%H-%M-%S")
log_file  <- here::here(glue("outputs/logs/run_all_errors_{timestamp}.log"))
if (!dir.exists(dirname(log_file))) dir.create(dirname(log_file), recursive = TRUE)

# Helper: append a timestamped error message to the log file (portable version)
append_error <- function(text) {
  con <- file(log_file, open = "a")
  writeLines(text, con)
  close(con)
}

# --- 4. Safely run a script and handle errors --------------------------------
run_script <- function(file) {
  file_path <- here::here(file)
  message(glue("\n--- Running: {file_path} ---"))
  
  tryCatch({
    # (a) Check required data for Study 1 cleaning
    if (grepl("01-study-01-cleaning\\.R$", file)) {
      raw_data <- here::here("data/raw/fr_cdsp_ddi_elipss_202312_bee.csv")
      if (!file.exists(raw_data)) {
        stop(glue(
          "Required data file not found: {raw_data}\n",
          "Please download it manually from https://doi.org/10.21410/7E4/OH0RKI ",
          "and place it in the 'data/raw/' folder."
        ))
      }
    }
    
    # (b) Execute script in an isolated environment
    source(file_path, echo = TRUE, max.deparse.length = Inf, local = new.env())
    
    # (c) Success feedback
    message(glue("Completed: {file}"))
    TRUE
    
  }, error = function(e) {
    # Log error with timestamp
    msg <- glue("[{format(Sys.time(), '%Y-%m-%d %H:%M:%S')}] Error in {file}: {e$message}")
    message(msg)
    append_error(msg)
    FALSE
  })
}

# --- 5. Define scripts to run -------------------------------------------------
scripts <- c(
  "code/01-study-01-cleaning.R",
  "code/02-study-01-descriptives.R",
  "code/03-study-01-analysis.R",
  "code/04-study-03-cleaning.R",
  "code/05-study-03-descriptives.R",
  "code/06-study-03-analysis.R"
)

# --- 6. Execute all scripts ---------------------------------------------------
results <- purrr::map_lgl(scripts, run_script)

# --- 7. Summarize results -----------------------------------------------------
n_success <- sum(results)
n_total   <- length(results)

message("\n------------------------------------------")
message(glue("===== Replication pipeline finished ({n_success}/{n_total} scripts ran successfully) ====="))

if (n_success < n_total) {
  message(glue("Some scripts failed. Check log: {log_file}"))
} else {
  message("All scripts completed successfully.")
}
